package com.bezdelnick.forum.web.component;

import com.bezdelnick.forum.domain.Question;
import com.bezdelnick.forum.service.ForumService;
import com.bezdelnick.forum.web.page.AddQuestionPage;
import com.bezdelnick.forum.web.page.ModeratorPage;
import com.bezdelnick.forum.web.page.ShowQuestionPage;
import com.bezdelnick.forum.web.page.ShowUserPage;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 25.07.12
 * Time: 15:37
 * To change this template use File | Settings | File Templates.
 */
public class QuestionsListView extends ListView {
    private static final Logger LOGGER = LoggerFactory.getLogger(QuestionsListView.class);

    @SpringBean
    @SuppressWarnings("unused")
    private ForumService forumService;

    private ForumService.MODE currentMode;

    public QuestionsListView(String id, List list, ForumService.MODE mode) {
        super(id, list);
        this.currentMode = mode;
    }

    public QuestionsListView(String id, IModel iModel, ForumService.MODE mode) {
        super(id, iModel);
        this.currentMode = mode;
    }

    @Override
    protected void populateItem(ListItem listItem) {
        final Question question = (Question) listItem.getModelObject();

        if (currentMode.equals(ForumService.MODE.ADMIN)) {
            // Action's links
            BookmarkablePageLink bplActionEdit = new BookmarkablePageLink("action_1", AddQuestionPage.class) {
                @Override
                public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                    replaceComponentTagBody(markupStream, openTag, "Edit");
                }
            };
            bplActionEdit.getPageParameters().add("action", ModeratorPage.ACTION_EDIT);
            bplActionEdit.getPageParameters().add("id", question.getId());
            listItem.add(bplActionEdit);

            BookmarkablePageLink bplActionRemove = new BookmarkablePageLink("action_2", ModeratorPage.class) {
                @Override
                public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                    replaceComponentTagBody(markupStream, openTag, "Remove");
                }
            };
            bplActionRemove.getPageParameters().add("action", ModeratorPage.ACTION_REMOVE);
            bplActionRemove.getPageParameters().add("id", question.getId());
            listItem.add(bplActionRemove);
        }

        BookmarkablePageLink bplQuestionTitle = new BookmarkablePageLink("cell_1", ShowQuestionPage.class) {
            @Override
            public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                replaceComponentTagBody(markupStream, openTag, question.getTitle());
            }
        };
        bplQuestionTitle.getPageParameters().add("id", question.getId());

        BookmarkablePageLink bplAuthorTitle = new BookmarkablePageLink("cell_2", ShowUserPage.class) {
            @Override
            public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                replaceComponentTagBody(markupStream, openTag,
                        forumService.getAppUserDao().getUserById(
                                question.getUserId()
                        ).getUsername());
            }


        };
        bplAuthorTitle.getPageParameters().add("id", question.getUserId());

        Label typeQuestion = new Label("cell_3",
                forumService.getTypeDao().getTypeById(
                        question.getTypeId()
                ).getName()
        );

        Label subtypeQuestion = new Label("cell_4",
                forumService.getSubtypeDao().getSubtypeById(
                        question.getSubtypeId()
                ).getName()
        );

        listItem.add(bplQuestionTitle);
        listItem.add(bplAuthorTitle);
        listItem.add(typeQuestion);
        listItem.add(subtypeQuestion);

        /*listItem.add(new ListView("cells", keys) { //keys

            @Override
            protected void populateItem(ListItem item) {

                switch (item.getIndex()) {
                    case 0:
                        BookmarkablePageLink bplQuestionTitle = new BookmarkablePageLink("cell_1", ShowQuestionPage.class){
                            @Override
                            public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                                replaceComponentTagBody(markupStream, openTag, question.getTitle());
                            }
                        };
                        bplQuestionTitle.getPageParameters().add("id", question.getId());
                        item.add(bplQuestionTitle);

                        item.add(new Label("cell_2", ""));
                        break;
                    case 1:
                        item.add(new Label("cell_1", forumService.getAppUserDao().getUserById(question.getUserId()).getUsername()));
                        item.add(new Label("cell_2", question.getDatePublication().toString()));
                        break;
                    case 2:
                        BookmarkablePageLink bplActionEdit = new BookmarkablePageLink("cell_1", ModeratorPage.class){
                            @Override
                            public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                                replaceComponentTagBody(markupStream, openTag, "Edit");
                            }
                        };
                        bplActionEdit.getPageParameters().add("action", ACTION_EDIT);
                        bplActionEdit.getPageParameters().add("id", question.getId());
                        item.add(bplActionEdit);

                        BookmarkablePageLink bplActionRemove = new BookmarkablePageLink("cell_2", ModeratorPage.class){
                            @Override
                            public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                                replaceComponentTagBody(markupStream, openTag, "Remove");
                            }
                        };
                        bplActionRemove.getPageParameters().add("action", ACTION_REMOVE);
                        bplActionRemove.getPageParameters().add("id", question.getId());
                        item.add(bplActionRemove);
                }
            }
        }

        );*/
    }
}
